package com.hm.hall.user.service;

import com.hm.hall.commons.dao.UserActionMapper;
import com.hm.hall.commons.domain.UserAction;
import com.hm.hall.commons.constans.ValueConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户动作,行为相关
 */
@Service
public class UserActionService {

    @Autowired
    private UserActionMapper userActionMapper;

    public UserAction getUserAction(long userId) {
        UserAction record = userActionMapper.selectByPrimaryKey(userId);
        if (record == null) {
            record = new UserAction();
            record.setUserId(userId);
            record.setCardStyle("");
            record.setTotalHand(ValueConstants.DEFAULT_ZERO);
            record.setTotalScoreBuy(ValueConstants.DEFAULT_ZERO);
            record.setTotalField(ValueConstants.DEFAULT_ZERO);
            record.setFieldHighestWin(ValueConstants.DEFAULT_ZERO);
            record.setHandHighestWin(ValueConstants.DEFAULT_ZERO);
            record.setMaximumBrand("[]");
            record.setTotalScore(ValueConstants.DEFAULT_ZERO);
            userActionMapper.insertSelective(record);
            return userActionMapper.selectByPrimaryKey(userId);
        }
        return record;
    }

    public int updateByPrimaryKeySelective(UserAction record) {
        return userActionMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 更新用户总局数信息
     *
     * @param userId
     * @param hand
     * @param field      每局坐下发牌才传1
     * @param filedScore 本场分数要在房间结束之后上传
     * @param handScore  本手分数
     * @param scoreBuy   公告牌
     * @param publicCard 手牌
     * @param handCard
     * @return
     */
    public boolean updateUserAction(long userId, int hand, int filedScore, int handScore, int scoreBuy,
                                    List<Short> publicCard, List<Short> handCard) {
        UserAction userAction = getUserAction(userId);
        userAction.setTotalHand(userAction.getTotalHand() + hand);
        userAction.setTotalScoreBuy(userAction.getTotalScoreBuy() + scoreBuy);
        // userAction.setTotalField(userAction.getTotalField() + field);
        userAction.setTotalScore(userAction.getTotalScore() + handScore);
        if (filedScore > userAction.getFieldHighestWin()) {
            userAction.setFieldHighestWin(filedScore);
        }
        if (handScore > userAction.getHandHighestWin()) {
            userAction.setHandHighestWin(handScore);
        }
        return userActionMapper.updateByPrimaryKeySelective(userAction) > ValueConstants.DEFAULT_ZERO;
    }
}
